package com.bitsemart.dialogos;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;

import com.bitsemart.MainActivity;
import com.bitsemart.R;

import java.util.concurrent.ExecutionException;

/**
 * Created by Roberto on 20/05/2015.
 */
public class AskDialogFragment  extends DialogFragment {
    public final static int TYPE_DELETE_GAME = 0;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View promptsView = getActivity().getLayoutInflater().inflate(R.layout.ask_dialog, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        int type = getArguments().getInt("TYPE");
        switch (type){
            case TYPE_DELETE_GAME: // Borrar Juego
                final int id_juego = getArguments().getInt("ID");
                final String titulo = getArguments().getString("TITLE");
                builder
                        .setTitle("¿Desea borrar " + titulo + " de la base de datos?")
                        .setView(promptsView)
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // No hacer nada
                            }
                        })
                        .setPositiveButton("Si", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                try {
                                    MainActivity.getBDHendrix().eliminarJuego(id_juego);
                                    MainActivity.mostrarMensaje(getActivity(),"Se ha borrado " + titulo +" correctamente");
                                } catch (InterruptedException e) {
                                    MainActivity.mostrarMensaje(getActivity(),"Error al guardar juego");
                                } catch (ExecutionException e) {
                                    MainActivity.mostrarMensaje(getActivity(),"Error al guardar juego");
                                }
                            }
                        });
            break;
        }

        return builder.create();
    }
}
